/**
 * 生成随机图表id
 */
function generateRandomKey(prefix) {
  if (prefix == null) {
    prefix = 'chart'
  }

  var userAgent = window.navigator.userAgent
    .replace(/[^a-zA-Z0-9]/g, '')
    .split('')
  var mid = ''
  for (var i = 0; i < 12; i++) {
    mid += userAgent[Math.round(Math.random() * (userAgent.length - 1))]
  }
  var time = new Date().getTime()

  return prefix + '_' + mid + '_' + time
}
/**
 * 深度克隆数据,包括对象，数组，map
 * @param {*} obj 对象，数组，map
 */
function deepCopy(obj) {
  if (!isObject(obj) && !isMap(obj)) {
    return obj
  }

  let cloneObj
  if (isMap(obj)) {
    cloneObj = new Map()
    for (let key of obj.keys()) {
      let value = obj.get(key)
      if (isMap(value) || isObject(value) || Array.isArray(obj)) {
        let copyVal = deepCopy(value)
        cloneObj.set(key, copyVal)
      } else {
        cloneObj.set(key, value)
      }
    }
  } else if (typeof obj === 'function') {
    cloneObj = obj
  } else {
    cloneObj = Array.isArray(obj) ? [] : {}
    if (obj instanceof HTMLElement) {
      cloneObj = obj.cloneNode(true)
    } else {
      for (let key in obj) {
        // if (obj.hasOwnProperty(key)) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          cloneObj[key] =
            isMap(obj[key]) || isObject(obj[key])
              ? deepCopy(obj[key])
              : obj[key]
        }
      }
    }
  }
  return cloneObj
}

/**
 * 判断参数是否是Object类型
 * @param {*} o
 */
function isObject(o) {
  return (
    !isMap(o) &&
    (typeof o === 'object' || typeof o === 'function') &&
    o !== null
  )
}

/**
 * 判断参数是否是Map类型
 * @param {*} obj
 */
function isMap(obj) {
  if (obj instanceof Map) {
    return true
  } else {
    return false
  }
}

// 替换temp中的${xxx}为指定内容 ,temp:字符串，这里指html代码，dataarry：一个对象{"xxx":"替换的内容"}
// 例：luckysheet.replaceHtml("${image}",{"image":"abc","jskdjslf":"abc"})   ==>  abc
function replaceHtml(temp, dataarry) {
  return temp.replace(/\$\{([\w]+)\}/g, function(s1, s2) {
    var s = dataarry[s2]
    if (typeof s != 'undefined') {
      return s
    } else {
      return s1
    }
  })
}

function hasChinaword(s) {
  var patrn = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi
  if (!patrn.exec(s)) {
    return false
  } else {
    return true
  }
}

export { isMap, isObject, deepCopy, generateRandomKey, replaceHtml }
